package com.szm.lnr;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import com.szm.lnr.base.BaseActivity;
import com.szm.lnr.utils.MainLogicUtil;

/**
 * Created by szm on 2018/7/27.
 */

public class ActionActivity extends BaseActivity {

    private Handler mHandler;
    private MainLogicUtil mMainLogicUtil;

    private int touchType;
    private boolean isCreate;

    private SurfaceView mSurfaceView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action);
        initActivity();
    }

    private void initActivity(){
        isCreate = true;
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case 0:
                        Toast.makeText(ActionActivity.this, "游戏结束！", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
        mSurfaceView = findTargetViewById(R.id.surface_view);
        mMainLogicUtil = new MainLogicUtil.Builder(mSurfaceView)
                .setHandler(mHandler)
                .setPadding(60).build();

        mSurfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                float x = motionEvent.getX();
                float y = motionEvent.getY();
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        if (y < view.getMeasuredHeight() / 4){
                            touchType = 0;
                        }else if (y > view.getMeasuredHeight() * 3 / 4){
                            touchType = 3;
                        }else if (x <= view.getMeasuredWidth() / 2){
                            touchType = 1;
                        }else {
                            touchType = 2;
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        if (y < view.getMeasuredHeight() / 4 && touchType == 0){
                            mMainLogicUtil.transformation();
                        }
                        if (x <= view.getMeasuredWidth() / 2 && touchType == 1){
                            mMainLogicUtil.moveToLeft();
                        }
                        if (x > view.getMeasuredWidth() / 2 && touchType == 2){
                            mMainLogicUtil.moveToRight();
                        }
                        if (y > view.getMeasuredHeight() * 3 / 4 && touchType == 3){
                            mMainLogicUtil.setCurShapeFast(true);
                        }
                        break;
                }
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (isCreate){
            mMainLogicUtil.start();
            isCreate = false;
        }else {
            mMainLogicUtil.setPuase(false);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMainLogicUtil.setPuase(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMainLogicUtil.stop();
    }
}
